<template>
  <div class="com-container" @dblclick="toggleChina">
    <div class="com-chart" ref="map_ref"></div>
  </div>
</template>

<script>
import { getMapData } from '@/API/api.js'
import axios from 'axios'
import { getProvinceMapInfo } from '@/utils/map_utils.js'
export default {
  name: 'Map',
  data() {
    return {
      myChart: null,
      // 散点图所有数据
      allData: null,
      // 缓存地图
      mapData: {}
    }
  },
  mounted() {
    this.initChart()
    this.getData()
    window.addEventListener('resize', this.screenAdater)
    this.screenAdater()
  },
  destroyed() {
    window.removeEventListener('resize', this.screenAdater)
  },
  methods: {
    // 初始化图表
    async initChart() {
      this.myChart = this.$echarts.init(this.$refs.map_ref, 'chalk')
      // 获取地图数据
      const { data: map } = await axios.get(
        'http://localhost:8999/static/map/china.json#/'
      )
      // 注册地图数据
      this.$echarts.registerMap('china', map)
      // 初始化配置对象
      const initOption = {
        title: {
          text: '▎商家分布',
          left: 20,
          top: 20
        },
        geo: {
          type: 'map',
          map: 'china',
          top: '5%',
          bottom: '5%',
          itemStyle: {
            areaColor: '#2E72BF',
            borderColor: '#333'
          }
        },
        legend: {
          left: '5%',
          bottom: '5%',
          orient: 'vertical'
        }
      }
      // 将配置对象添加到echarts初始化对象上
      this.myChart.setOption(initOption)
      // 点击省份显示省份地图
      this.myChart.on('click', async(arg) => {
        // console.log(arg)
        // 获取省份地图数据
        const mapInfo = getProvinceMapInfo(arg.name)
        console.log(mapInfo)
        if (!this.mapData[mapInfo.key]) {
          const { data: map } = await axios.get(
            'http://localhost:8999' + mapInfo.path
          )
          console.log(map)
          this.mapData[mapInfo.key] = map
          // 注册省份地图数据
          this.$echarts.registerMap(mapInfo.key, map)
        }
        const clickChangeMapOption = {
          geo: {
            map: mapInfo.key
          }
        }
        this.myChart.setOption(clickChangeMapOption)
      })
    },
    // 获取数据
    async getData() {
      const { data: res } = await getMapData()
      console.log(res)
      this.allData = res
      this.updateChart()
    },
    // 处理数据更新图表
    updateChart() {
      // 图例数据
      const legendArr = this.allData.map((item) => {
        return item.name
      })
      const seriesArr = this.allData.map((item) => {
        return {
          type: 'effectScatter',
          rippleEffect: {
            scale: 5,
            brushType: 'stroke'
          },
          name: item.name,
          data: item.children,
          coordinateSystem: 'geo'
        }
      })
      console.log(seriesArr)
      const dataOption = {
        legend: {
          data: legendArr
        },
        series: seriesArr
      }
      this.myChart.setOption(dataOption)
    },
    // 分辨率适配
    screenAdater() {
      const titleFontSize = (this.$refs.map_ref.offsetWidth / 100) * 3.6
      const adaterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize
          }
        },
        legend: {
          itemWidth: titleFontSize / 2,
          itemHeight: titleFontSize / 2,
          itemGap: titleFontSize / 2,
          textStyle: {
            fontSize: titleFontSize / 3
          }
        }
      }
      this.myChart.setOption(adaterOption)
      this.myChart.resize()
    },
    // 双击切换为中国地图
    toggleChina() {
      const toggleChinaOption = {
        geo: {
          map: 'china'
        }
      }
      this.myChart.setOption(toggleChinaOption)
    }
  }
}
</script>

<style lang="scss" scoped></style>
